package com.zenithmind.coding.pojo.entity;

import com.baomidou.mybatisplus.annotation.TableName;
import com.zenithmind.common.base.BaseEntity;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.util.Objects;

/**
 * 编程问题统计信息实体类 - 遵循单一职责原则，专门处理问题的统计数据
 * 
 * @author ZenithMind Team
 * @since 2025-01-09
 */
@Data
@EqualsAndHashCode(callSuper = true, onlyExplicitlyIncluded = true)
@TableName("coding_problem_statistics")
@Schema(description = "编程问题统计信息实体")
public class ProblemStatistics extends BaseEntity {

    /**
     * 问题ID
     */
    @Schema(description = "问题ID")
    @EqualsAndHashCode.Include
    private String problemId;

    /**
     * 提交次数
     */
    @Schema(description = "提交次数")
    private Long submitCount;

    /**
     * 通过次数
     */
    @Schema(description = "通过次数")
    private Long acceptCount;

    /**
     * 通过率
     */
    @Schema(description = "通过率")
    private Double acceptRate;

    /**
     * 平均用时（毫秒）
     */
    @Schema(description = "平均用时（毫秒）")
    private Integer avgTime;

    /**
     * 平均内存（KB）
     */
    @Schema(description = "平均内存（KB）")
    private Integer avgMemory;

    /**
     * 问题评分
     */
    @Schema(description = "问题评分")
    private Double rating;

    /**
     * 最佳用时（毫秒）
     */
    @Schema(description = "最佳用时（毫秒）")
    private Integer bestTime;

    /**
     * 最佳内存（KB）
     */
    @Schema(description = "最佳内存（KB）")
    private Integer bestMemory;

    /**
     * 今日提交次数
     */
    @Schema(description = "今日提交次数")
    private Long todaySubmitCount;

    /**
     * 今日通过次数
     */
    @Schema(description = "今日通过次数")
    private Long todayAcceptCount;

    /**
     * 本周提交次数
     */
    @Schema(description = "本周提交次数")
    private Long weekSubmitCount;

    /**
     * 本周通过次数
     */
    @Schema(description = "本周通过次数")
    private Long weekAcceptCount;

    /**
     * 本月提交次数
     */
    @Schema(description = "本月提交次数")
    private Long monthSubmitCount;

    /**
     * 本月通过次数
     */
    @Schema(description = "本月通过次数")
    private Long monthAcceptCount;

    /**
     * 热度分数
     */
    @Schema(description = "热度分数")
    private Double hotScore;

    /**
     * 难度分数（基于用户反馈计算）
     */
    @Schema(description = "难度分数（基于用户反馈计算）")
    private Double difficultyScore;

    /**
     * 重写equals方法以确保里氏替换原则
     */
    @Override
    public boolean equals(Object obj) {
        if (this == obj) return true;
        if (obj == null || getClass() != obj.getClass()) return false;
        if (!super.equals(obj)) return false;
        
        ProblemStatistics that = (ProblemStatistics) obj;
        return Objects.equals(problemId, that.problemId);
    }

    /**
     * 重写hashCode方法以确保里氏替换原则
     */
    @Override
    public int hashCode() {
        return Objects.hash(super.hashCode(), problemId);
    }

    /**
     * 计算通过率
     */
    public void calculateAcceptRate() {
        if (submitCount != null && submitCount > 0 && acceptCount != null) {
            this.acceptRate = (double) acceptCount / submitCount * 100;
        } else {
            this.acceptRate = 0.0;
        }
    }

    /**
     * 更新统计数据
     */
    public void updateStatistics(Long submitIncrement, Long acceptIncrement) {
        if (submitIncrement != null && submitIncrement > 0) {
            this.submitCount = (this.submitCount == null ? 0 : this.submitCount) + submitIncrement;
        }
        if (acceptIncrement != null && acceptIncrement > 0) {
            this.acceptCount = (this.acceptCount == null ? 0 : this.acceptCount) + acceptIncrement;
        }
        calculateAcceptRate();
    }

    /**
     * 更新性能数据
     */
    public void updatePerformance(Integer time, Integer memory) {
        if (time != null && time > 0) {
            if (this.bestTime == null || time < this.bestTime) {
                this.bestTime = time;
            }
            // 更新平均时间（简化计算）
            if (this.avgTime == null) {
                this.avgTime = time;
            } else {
                this.avgTime = (this.avgTime + time) / 2;
            }
        }
        
        if (memory != null && memory > 0) {
            if (this.bestMemory == null || memory < this.bestMemory) {
                this.bestMemory = memory;
            }
            // 更新平均内存（简化计算）
            if (this.avgMemory == null) {
                this.avgMemory = memory;
            } else {
                this.avgMemory = (this.avgMemory + memory) / 2;
            }
        }
    }
}
